package dao;

import entity.Order;
import entity.OrderDetail;
import utils.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    public int createOrder(int userId, double totalPrice, String address) {
        String sql = "INSERT INTO orders (user_id, total_price, status,address) VALUES (?, ?, '已提交',?)";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            // 设置占位符参数
            pstmt.setInt(1, userId);
            pstmt.setDouble(2, totalPrice);
            pstmt.setString(3, address);

            // 执行插入操作
            int rowsAffected = pstmt.executeUpdate();
            if (rowsAffected > 0) {
                // 获取生成的订单 ID
                try (ResultSet rs = pstmt.getGeneratedKeys()) {
                    if (rs.next()) {
                        return rs.getInt(1); // 返回订单 ID
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return -1; // 插入失败，返回 -1
    }

    public List<Order> getOrdersByUserId(int userId) {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM orders WHERE user_id = ?";

        // 获取数据库连接
        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            // 设置查询参数
            preparedStatement.setInt(1, userId);

            // 执行查询
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                // 处理查询结果
                while (resultSet.next()) {
                    Order order = new Order();
                    order.setOrderId(resultSet.getInt("order_id"));
                    order.setUserId(resultSet.getInt("user_id"));
                    order.setTotalPrice(resultSet.getBigDecimal("total_price"));
                    order.setStatus(resultSet.getString("status"));

                    order.setAddress(resultSet.getString("address")); // 假设地址字段存在

                    // 将订单加入列表
                    orders.add(order);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 输出异常信息
        }

        return orders;
    }

    public boolean updateOrderStatus(int orderId, String status) {
        String sql = "UPDATE orders SET status = ? WHERE order_id = ?";

        // 获取数据库连接
        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            // 设置查询参数
            preparedStatement.setString(1, status); // 设置订单状态
            preparedStatement.setInt(2, orderId);   // 设置订单ID

            // 执行更新操作
            int rowsAffected = preparedStatement.executeUpdate();

            // 返回是否更新成功
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace(); // 输出异常信息
            return false; // 如果发生异常，则返回false
        }
    }


    public List<Order> getOrdersByShopUserId(int userId) {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT DISTINCT o.*\n" +
                "FROM orders o\n" +
                "JOIN order_details od ON o.order_id = od.order_id\n" +
                "JOIN products p ON od.product_id = p.PRODUCT_ID\n" +
                "WHERE p.USER_ID = ? ;\n";

        // 获取数据库连接
        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            // 设置查询参数
            preparedStatement.setInt(1, userId);

            // 执行查询
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                // 处理查询结果
                while (resultSet.next()) {
                    Order order = new Order();
                    order.setOrderId(resultSet.getInt("order_id"));
                    order.setUserId(resultSet.getInt("user_id"));
                    order.setTotalPrice(resultSet.getBigDecimal("total_price"));
                    order.setStatus(resultSet.getString("status"));

                    order.setAddress(resultSet.getString("address")); // 假设地址字段存在

                    // 将订单加入列表
                    orders.add(order);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 输出异常信息
        }

        return orders;
    }
}
